package com.ocean.courtyard.core.datasource;

import com.ocean.courtyard.CourtyardApplication;
import com.ocean.courtyard.core.datasource.mapper.WorldMapper;
import com.ocean.courtyard.entity.CourtyardHome;
import com.ocean.courtyard.exception.HomeNotFoundException;
import lombok.Getter;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

public class SqlSessionImpl {

    @Getter
    private final SqlSessionFactory sqlSessionFactory;

    private final JavaPlugin plugin;

    public SqlSessionImpl(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
        this.plugin = CourtyardApplication.getInstance();
    }

    public void initialize() {
        try (SqlSession session = sqlSessionFactory.openSession()) {
            WorldMapper mapper = session.getMapper(WorldMapper.class);
            mapper.initializeCourtyardHome();
            mapper.initializeCourtyardAllow();
            session.commit();
        }
    }

    public void addHome(CourtyardHome home) {
        try (SqlSession session = sqlSessionFactory.openSession()) {
            WorldMapper mapper = session.getMapper(WorldMapper.class);
            mapper.addHome(home);
            session.commit();
        }
    }

    public Integer getHomeNumber(String name) {
        try (SqlSession session = sqlSessionFactory.openSession()) {
            WorldMapper mapper = session.getMapper(WorldMapper.class);
            Integer count = mapper.getHomeNumber(name);
            session.commit();
            return count;
        }
    }

    public CourtyardHome getHome(String name) {
        try (SqlSession session = sqlSessionFactory.openSession()) {
            WorldMapper mapper = session.getMapper(WorldMapper.class);
            return mapper.getHome(name);
        }
    }

    public void tpSet(String username, double x, double y, double z) {
        try (SqlSession session = sqlSessionFactory.openSession()) {
            WorldMapper mapper = session.getMapper(WorldMapper.class);
            mapper.tpSet(username, x, y, z);
            session.commit();
        }
    }

    public CourtyardHome getHome(Player player) {
        CourtyardHome home = this.getHome(player.getName());
        if (home != null) {
            return home;
        }
        throw new HomeNotFoundException("Home Not Found: " + player.getName());
    }

    public void addPlayerToHome(Long homeId, String name) {
        try (SqlSession session = sqlSessionFactory.openSession()) {
            WorldMapper mapper = session.getMapper(WorldMapper.class);
            mapper.addPlayerToHome(homeId, name);
            session.commit();
        }
    }

    public Integer checkPlayerExists(Long homeId, String targetName) {
        try (SqlSession session = sqlSessionFactory.openSession()) {
            WorldMapper mapper = session.getMapper(WorldMapper.class);
            return mapper.checkPlayerExists(homeId, targetName);
        }
    }

    public CourtyardHome getHomeByWorldName(String toWorldName) {
        try (SqlSession session = sqlSessionFactory.openSession()) {
            WorldMapper mapper = session.getMapper(WorldMapper.class);
            return mapper.getHomeByWorldName(toWorldName);
        }
    }

    public Integer getPlayerAllowCount(Long homeId) {
        try (SqlSession session = sqlSessionFactory.openSession()) {
            WorldMapper mapper = session.getMapper(WorldMapper.class);
            return mapper.getPlayerAllowCount(homeId);
        }
    }
}
